package com.itboye.jigongbao.widget;

import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.jigongbao.R;
import com.itboye.jigongbao.fragments.MeFragment;

/**
 * Created by Administrator on 2018/1/12.
 */

public class SharePopupWindow extends PopupWindow {
    private TextView txt_weixin;
    private View mPopView;

    public SharePopupWindow(Fragment context) {
        super(context.getActivity());
        // TODO Auto-generated constructor stub
        init(context);
        setPopupWindow();

    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Fragment context) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(context.getActivity());
        //绑定布局
        mPopView = inflater.inflate(R.layout.popup_share, null);
        txt_weixin = mPopView.findViewById(R.id.txt_weixin);
        txt_weixin.setOnClickListener((MeFragment)context);
    }

    /**
     * 设置窗口的相关属性
     */
    @SuppressLint("InlinedApi")
    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.mypopwindow_anim_style);// 设置动画
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        mPopView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mPopView.findViewById(R.id.id_pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
